import {
  listUserAirplanRelationApi,
  addlistUserAirplanRelationApi,
  getTailNumberAndFleetApi,
  dellistUserAirplanRelationApi
} from '@/services/userAirplaneRelation.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    userAirplanRelationLoading: false,
    userAirplanRelation: [],
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    addLoading: false,
    tailNumberAndFleet: [],
    tailNumberAndFleetUserName: []
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取用户list
    async getListUserAirplanRelation({ state, commit }, params) {
      commit('setState', { key: 'userAirplanRelationLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await listUserAirplanRelationApi(apiParams);
      commit('setState', { key: 'userAirplanRelation', payload: rep.data.list });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'userAirplanRelationLoading', payload: false });
    },
    async getTailNumberAndFleet({ state, commit }, params = {}) {
      const rep = await getTailNumberAndFleetApi(params);
      if (params.userName) {
        commit('setState', { key: 'tailNumberAndFleetUserName', payload: rep.data });
      } else {
        commit('setState', { key: 'tailNumberAndFleet', payload: rep.data });
      }
    },
    // 新增
    async addlistUserAirplanRelation({ commit }, params) {
      commit('setState', { key: 'addLoading', payload: true });
      try {
        const rep = await addlistUserAirplanRelationApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addLoading', payload: false });
      }
    },
    async dellistUserAirplanRelation({ commit }, params) {
      commit('setState', { key: 'addLoading', payload: true });
      try {
        const rep = await dellistUserAirplanRelationApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addLoading', payload: false });
      }
    }
  }
};
